package com.zy.explore.sys.infrastructure.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * description Knife4j配置类
 *
 * @author 🇨🇳中国印🇨🇳 zyexplorej2ee@sina.com
 * @date 2022年01月17日 21:51:56
 **/
@Configuration("zy-sys-Knife4jConfig")
@EnableSwagger2
@EnableKnife4j
@Profile("!prod")
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {

    private static final String PREFIX = "zy-sys-";

    private static final String DESC = "系统模块-";

    private static final String BASE_PACKAGE = "com.zy.explore.sys.interfaces.facade";

    @Resource
    private OpenApiExtensionResolver openApiExtensionResolver;

    /**
     * 所有接口
     */
    @Bean(value = PREFIX + "apiAll")
    public Docket apiAll() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(DESC + "所有接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                .paths(PathSelectors.any())
                .build().extensions(openApiExtensionResolver.buildExtensions(DESC + "所有接口"));
    }

    /**
     * API分组 示例接口
     */
    @Bean(value = PREFIX + "example")
    public Docket exampleApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(DESC + "示例接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE + ".example"))
                .paths(PathSelectors.any())
                .build().extensions(openApiExtensionResolver.buildExtensions(DESC + "示例接口"));
    }

    /**
     * API分组 版本1
     */
    @Bean(value = PREFIX + "apiV1")
    public Docket apiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(DESC + "v1版")
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE + ".v1"))
                .paths(PathSelectors.any())
                .build().extensions(openApiExtensionResolver.buildExtensions(DESC + "v1版"));
    }

    /**
     * API分组 版本2
     */
    @Bean(value = PREFIX + "apiV2")
    public Docket apiV2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(DESC + "v2版")
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE + ".v2"))
                .paths(PathSelectors.any())
                .build().extensions(openApiExtensionResolver.buildExtensions(DESC + "v2版"));
    }

    /**
     * api的信息
     *
     * @return {@link ApiInfo }
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Restful API 说明文档")
                .contact(new Contact("🇨🇳中国印🇨🇳", "https://gitee.com/zy-explorej2ee", "zyexplorej2ee@sina.com"))
                .description("基于Swagger2和Knife4j实现的接口文档")
                .version("1.0")
                .build();
    }
}